package io.geekidea.springboot.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springboot.entity.Building;
import io.geekidea.springboot.service.IBuildingService;
import io.geekidea.springboot.vo.ExportPo;
import io.geekidea.springboot.vo.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/building")
@Tag(name = "设施管理", description = "设施信息管理接口")
public class BuildingController {

    @Autowired
    private IBuildingService buildingService;

    @PostMapping
    @Operation(summary = "新增设施")
    public ResponseResult<Building> add(@Valid @RequestBody Building building) {
        building.setId(null);
        buildingService.save(building);
        return ResponseResult.success(building);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除设施")
    public ResponseResult<Void> delete(@PathVariable Long id) {
        buildingService.delete(id);
        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "更新设施")
    public ResponseResult<Building> update(@Valid @RequestBody Building building) {
        buildingService.updateById(building);
        return ResponseResult.success(building);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询设施详情")
    public ResponseResult<Building> getById(@PathVariable Long id) {
        Building building = buildingService.getById(id);
        return ResponseResult.success(building);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询设施列表")
    public ResponseResult<Page<Building>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String types) {

        List<Long> typesList = null; // 初始化类型列表

        // 检查 types 是否不为空
        if (StrUtil.isNotBlank(types)) {
            // 将 types 字符串分割并转换为 Long 类型的列表
            typesList = Arrays.stream(types.split(","))
                    .map(Long::parseLong) // 转换为 Long
                    .collect(Collectors.toList());
        }

        // 调用服务方法，传递 typesList
        Page<Building> result = buildingService.listByPage(page, size, typesList);
        return ResponseResult.success(result);
    }

    @PostMapping("/export")
    @Operation(summary = "导出设施信息")
    public void export(HttpServletResponse response,
                       @RequestBody ExportPo exportPo) throws IOException {
        List<Building> buildings = buildingService.getExportList(exportPo.getTypes(),exportPo.getBuildIds());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("设施信息", "设施信息"), Building.class, buildings);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=building_info.xlsx");
        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
